#!/bin/bash
#Author    : some-guy (Muhammed U)
#Copyright : (C) 2008 some-guy
#E-mail    : muhammedu (at) gmail.com

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# prepare packages for other uses ===
# SLAPT_GET_PARAMETERS must be provided, slax-get sets to --instal -d -p, by default
# TMP_DIR Must also be set, slax-get sets to /tmp/slax-get
# $* is the packages that are going to be downloaded/built
function prepare_pkg
{
if [ -z "$*" ]; then
	echo "Must provide packages"
	exit
fi
	SLAPT_GET_WORKING=`cat /etc/slapt-get/slapt-getrc | grep WORKINGDIR | sed -e s/WORKINGDIR// | sed -e s/=//`
	PACKAGE="$*"
	#Clean up any junk
	slapt-get --clean
	#Get packages
	slapt-get $SLAPT_GET_PARAMETERS $PACKAGE
	if [ $? -ne 0 ]; then
		exit 1
	else
		cd $SLAPT_GET_WORKING
	fi
	#Move and extract packages
	TGZS=`find . -noleaf | grep tgz`
	for t in $TGZS; do
		NAME=`echo $t | sed 's/.*\///g'`
		mkdir $TMP_DIR/$NAME-packagebuild
		echo "- Extracting $t"
		installpkg -root $TMP_DIR/$NAME-packagebuild/ $t >/dev/null
		cd $TMP_DIR/$NAME-packagebuild
		find ./ -noleaf -type d | xargs chmod -v 755 >/dev/null 2>&1
		find . -noleaf | grep .gz | xargs gunzip >/dev/null 2>&1
		find . -noleaf | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded >/dev/null 2>&1
		cd $SLAPT_GET_WORKING/
	done

	cd $TMP_DIR
	for x in `ls -1 | grep packagebuild`; do
		#Make modules
		pkgname=`echo -e $x | sed s/.tgz-packagebuild/-slaxget/`
		echo "- Building Module: $x"
		dir2lzm $x $HOME/Desktop/$pkgname.lzm >/dev/null
		rm -rf $x
		echo
	done
	cd $HOME
	rm -rf $TMP_DIR
}

# config file ===
# no arguments needed
function rc_file
{
if [ ! -e "/etc/slapt-get/slapt-getrc" ]; then
	echo -n "- Generating Config file"
	echo -n "."
	sleep 0.6
	echo "# See /usr/doc/slapt-get-0.9.12c/example.slapt-getrc " > /etc/slapt-get/slapt-getrc
	echo "# for example source entries and configuration hints." >> /etc/slapt-get/slapt-getrc
	echo "WORKINGDIR=/var/slapt-get" >> /etc/slapt-get/slapt-getrc
	echo "EXCLUDE=^kernel-.*,^alsa-.*,^glibc.*,.*-[0-9]+dl$,^devs$,^udev$,aaa_elflibs,x86_64" >> /etc/slapt-get/slapt-getrc
	#Add slackware repo with dep checking
	echo "SOURCE=http://www.stabellini.net/filesystem/slackware-12.0" >> /etc/slapt-get/slapt-getrc
	#Add slacky.eu
	echo "SOURCE=http://repository.slacky.eu/slackware-12.0/" >> /etc/slapt-get/slapt-getrc
	#Add linuxpackages.net
	echo "SOURCE=http://linuxpackages.cs.utah.edu//Slackware-12.0/" >> /etc/slapt-get/slapt-getrc
	echo -n "."
	sleep 0.6
	echo "."
	sleep 0.6
	echo "- Now run \"slax-get update\" to update the datebase, the config file is stored in /etc/slapt-get/slapt-getrc"
fi
}

# actiavte modules ===
# no arguments, actiavtes all .lzms on desktop
function install_pkg
{
	#Activate modules
	cd $HOME/Desktop/
	for e in `ls | grep slaxget`; do
		echo -e "- Activating $e"
		activate $e >/dev/null
	done
}

# remove packages ===
# $* is the packges to be removed
function remove_pkgs
{
if [ -z "$*" ]; then
	echo "Must provide packages"
	exit 1
fi

cd /mnt/live/memory/modules
for r in $*; do
	FOUND=`find . | grep $r`
	if [ ! -z $FOUND ]; then
		for t in $FOUND; do
			echo "- Removing $t"
			rm -rf $t
		done
	else
		echo "- No Matches for $r"
	fi
done
}
